<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/2/27-14:31
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;


    use app\admin\model\AdminUser;
    use app\admin\service\AdminUserService;
    use app\admin\service\RoleService;
    use Jrk\Trees;
    use Jrk\Util;

    class Admin extends AdminBase
    {

        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model=new AdminUser();
            $this->service=new AdminUserService();
            $this->del="管理员";
        }



        /**
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/27
         * @name: addUser
         * @describe:添加编辑
         */
        public function addUser(){
            if (IS_POST){
                $data = $this->request->post();
                $validate=new \app\admin\validate\AdminUser();
                if (!$validate->check($data)) {
                    $this->error($validate->getError());
                }
                if (isset($data['repassword'])){
                    unset($data['repassword']);
                }
                $data['password']=Util::think_encrypt($data['password']);

                if (isset($data['id'])){
                    parent::addLog("修改管理员信息","修改管理员：".$data['nickname']."信息");
                }else{
                    parent::addLog("添加管理员","添加管理员：".$data['nickname']);
                }
                return $this->model->addAndEdit($data);
            }

            $role=new RoleService();
            $da=$role->getRoleList();
            $this->assign("role",$da);
            return $this->fetch();
        }



        /**
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/27
         * @name: editUser
         * @describe:编辑
         */
        public function editUser(){
            $role=new RoleService();
            $roles=$role->whereNotIn("id","1")->selectOrFail();
            $id   = $this->request->param("id/d");//父id
            $info = $this->service->getOne($id);
            $info['password']=Util::think_decrypt($info['password']);
            $this->assign(compact("info","roles"));

            return $this->fetch();
        }



        /**
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/27
         * @name: tree
         * @describe:获取tree
         */
        public function tree(){
            if (IS_AJAX){
                $role=new RoleService();
                $da=$role->getRoleList();
                if (!empty($da)){
                    foreach($da as $k => $v){
                        $da[$k]['title'] = $v['name'];
                    }
                    $data = Trees::DeepTree($da);
                }else{
                    $data=[];
                }

                $this->ajaxResult($data,100,'');
            }
        }



    }